#!/usr/bin/env bash

VERSION="$1"

if [[ "${VERSION}" == "" ]] ; then
  echo -e "Usage: script/set-version X.Y.Z\n\nUpdates all Cargo.toml files with a new version."
  exit 3
fi

function die() {
  echo "$1"
  exit 2
}

which rg > /dev/null || die "Please 'cargo install ripgrep'"

CARGO_TOML_FILES=$(rg '^version =' -t toml -l)

for file in $CARGO_TOML_FILES ; do
  sed -E \
    -e "s/^version = \".*\"\$/version = \"${VERSION}\"/" \
    -e "s/(amethyst_.*\", )version = \"[0-9]+.[0-9]+.[0-9]+\"/\\1version = \"${VERSION}\"/g" \
    -e "s/^(amethyst = \\{ path = \"\.\.\", )version = \"[0-9]+.[0-9]+.[0-9]+\"/\\1version = \"${VERSION}\"/g" \
    -i "" "${file}"
done